/**
 * 
 */
package com.wisdytech.linkdcs.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.model.Client;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.system.dao.ISysOperationDao;
import com.wisdytech.linkdcs.system.model.SysOperation;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.system.service.ISystemOperationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * ClassName: SystemOperationServiceImpl
 * </p>
 * <p>
 * Description: 工序管理的业务实现层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-10-17
 * </p>
 */
@Service("systemOperationService")
@Transactional
public class SystemOperationServiceImpl extends BaseService<ISysOperationDao, SysOperation> implements ISystemOperationService {
     /**
      * 注入工序管理dao
      */
    @Resource
    private ISysOperationDao operationDao;
    /**
     * 注入设备dao
     */
    @Resource
    private IEqEquipmentDao equipmentDao;
    /**
     * 注入采集终端dao
     */
    @Resource
    private IEqClientDao clientDao;
     
    @Override
    public PageInfo<SysOperation> listSysOperationByFilter(String code, String name, Filter filter) {
        EntityWrapper<SysOperation> ewOperation = new EntityWrapper<>();
        if (StringUtils.isNotBlank(code)) {
            ewOperation.like("op_code", code);
        }
        if (StringUtils.isNotBlank(name)) {
            ewOperation.like("op_name", name);
        }
        ewOperation.orderBy("create_date", false);
        List<SysOperation> list = super.beforeList(ISysOperationDao.class, filter)
                .selectList(ewOperation.eq("del_flag", CommonConstants.DEL_FLAG));
        PageInfo<SysOperation> operationPage = new PageInfo<>(list);
        return operationPage;
    }

    @Override
    public StandardResult addSysOperation(SysOperation sysOperation, String loginName) {
        EntityWrapper<SysOperation> ew = new EntityWrapper<>();
        //验证工序编码的唯一性
        List<SysOperation> operationList = new ArrayList<>();
        operationList = operationDao.selectPage(new Page<SysUser>(1, 1), ew.eq("op_code", sysOperation.getCode()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!operationList.isEmpty()) {
            return StandardResult.getFailResult(CommonConstants.OPERATION_USE);
        }
        sysOperation.setPkId(BaseUtil.getUUID());
        sysOperation.setDelFlag(CommonConstants.DEL_FLAG);
        sysOperation.setCreateUser(loginName);
        sysOperation.setCreateDate(new Date());
        int operationCount = operationDao.insert(sysOperation);
        if (operationCount <= 0) {
            throw new ServiceException("新增失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult delSysOperationById(String ids) {
        List<String> idList = new ArrayList<String>();
        String[] id = ids.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        for (String pkId : id) {
            List<Equipment> equipmentList = equipmentDao.listAllEquipmentByOperationId(pkId);
            if (!equipmentList.isEmpty()) {
                return StandardResult.getFailResult(CommonConstants.OPERATION_EQUIPMENT);
            }
            List<Client> clientList = clientDao.listAllClientByOperationId(pkId);
            if (!clientList.isEmpty()) {
                return StandardResult.getFailResult(CommonConstants.OPERATION_CLIENT);
            }
        }
        int operationCount = operationDao.deleteBatchIds(idList);
        if (operationCount <= 0) {
            throw new ServiceException("删除失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateSysOperation(SysOperation sysOperation, String loginName) {
        EntityWrapper<SysOperation> ew =new EntityWrapper<>();
        //验证工序编码的唯一性--修改功能
        List<SysOperation> operationList;
        operationList = operationDao.selectPage(new Page<SysOperation>(1, 1),ew.eq("op_code", sysOperation.getCode()).ne("pk_id", sysOperation.getPkId()));
        if (operationList.size() > 0){
            return StandardResult.getFailResult(CommonConstants.OPERATION_USE);
        }
        sysOperation.setUpdateDate(new Date());
        sysOperation.setUpdateUser(loginName);
        int operationCount = operationDao.updateById(sysOperation);
        if (operationCount <= 0) {
            throw new ServiceException("修改失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public SysOperation getSysOperationById(String pkId) {
        return operationDao.selectById(pkId);
    }

    @Override
    public List<SysOperation> listAllOperation(String serach) {
        EntityWrapper<SysOperation> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(serach)) {
            ew.like("op_name", serach).or().like("op_code",serach);
        }
        ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
        return operationDao.selectList(ew);
    }

    @Override
    public List<SysOperation> listOperationBymodelCode(String search, String modelCode) {
        return operationDao.listOperationBymodelCode(search,modelCode);
    }
}
